package com.cgtong.android.mvp.model;

import android.text.TextUtils;

import com.blankj.utilcode.util.LogUtils;
import com.cgtong.android.bean.SimpleResult;
import com.cgtong.android.http.ApiManager;
import com.cgtong.android.http.callback.BaseCallBack;
import com.cgtong.android.mvp.contract.ReportProblemContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.body.UIProgressResponseCallBack;
import com.zhouyou.http.callback.ProgressDialogCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;
import com.zhouyou.http.request.PostRequest;
import com.zhouyou.http.subsciber.IProgressDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;

/**
 * Created by chunc on 2017/10/23.
 */

public class ReportProblemModel implements ReportProblemContract.Model {


    @Override
    public Disposable httpCommitReport(long problemId, String desc, String address, double lng, double lat, int isDraft, int isOpen, List<File> imgFiles, File posFile, File voiceFile,int voiceTime, final IProgressDialog progressDialog, final BaseCallBack<SimpleResult> baseCallBack) {
        UIProgressResponseCallBack progressResponseCallBack = new UIProgressResponseCallBack() {
            @Override
            public void onUIResponseProgress(long bytesRead, long contentLength, boolean done) {
//                int progress = (int) (bytesRead * 100 / contentLength);
//                HttpLog.i("progress=" + progress);
//                ((ProgressDialog) progressDialog.getDialog()).setProgress(progress);
//                if (done) {//完成
//                    ((ProgressDialog) progressDialog.getDialog()).setMessage("上传完整");
//                }
            }
        };

        PostRequest request = EasyHttp.post(ApiManager.PROBLEM_SAVE_PROBLEM_URL);
        //图片
        if (imgFiles.size() > 0) {
            for (File f :
                    imgFiles) {
                LogUtils.d("图片大小：" + f.length());
            }
            request.addFileParams("file", imgFiles, progressResponseCallBack);
        }

        //位置图片
        if (posFile != null) {
            LogUtils.d("posFile=====================================" + posFile.getAbsolutePath());
            LogUtils.d("位置图片大小：" + posFile.length());
            HttpParams posFileParams = new HttpParams();
            posFileParams.put("posfile", posFile, posFile.getName(), MediaType.parse("image/*"), progressResponseCallBack);
            request.params(posFileParams);
        }
        //录音
        if (voiceFile != null) {
            LogUtils.d("voicefile=====================================" + voiceFile.getAbsolutePath());
            LogUtils.d("voicetime=====================================" + voiceTime);
            LogUtils.d("录音大小：" + voiceFile.length());
            HttpParams voiceFileParams = new HttpParams();
            voiceFileParams.put("voicefile", voiceFile, voiceFile.getName(), progressResponseCallBack);
            voiceFileParams.put("voicetime", String.valueOf(voiceTime));
            request.params(voiceFileParams);
        }

        if (!TextUtils.isEmpty(desc)) {
            request.params("desc", desc);
        }
        if (problemId != -1) {
            request.params("problemid", String.valueOf(problemId));
        }
        LogUtils.d("desc = " + desc + " problemId = " + problemId + " address = " + address + " lng = " + lng + " lat = " + lat + " isdraft = " + isDraft + " isopen = " + isOpen);
       return request.params("address", address)
                .params("lng", String.valueOf(lng))
                .params("lat", String.valueOf(lat))
                .params("isdraft", String.valueOf(isDraft))
                .params("isopen", String.valueOf(isOpen))
                .accessToken(true)
                .execute(new ProgressDialogCallBack<String>(progressDialog, true, true) {
                    @Override
                    public void onError(ApiException e) {
                        baseCallBack.onError(e);
                    }

                    @Override
                    public void onSuccess(String apiResult) {
                        try {
                            JSONObject object = new JSONObject(apiResult);
                            SimpleResult result = new SimpleResult();
                            result.setCode(object.optInt("code"));
                            result.setMsg(object.optString("msg"));
                            baseCallBack.onSuccess(result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }


}
